const { writeFile, readFile, fsExistsSync } = require("../../app/util");
const { themeList } = require("../tool/util");
const defaultGlobal = require("../default/global");


function getLongestArray(arr1, arr2) {
    return arr1.length > arr2.length ? [arr1, arr2] : [arr2, arr1];
}

function findDifference(arr1, arr2) {
    let diff = arr1.filter((item) => {
        const index = arr2.findIndex((el) => el === item);
        if (index !== -1) {
            arr2.splice(index, 1);
            return false;
        }
        return true;
    });
    return { default: diff, template: arr2 };
}

function diffGlobal(arry1, arry2) {
    let findArray;

    if (arry1.length != arry2.length) {
        console.log(findDifference(arry1, arry2));
    }
}

function compareObjects(obj1, obj2) {
    const diff = {};

    for (let key in obj1) {
        if (obj1.hasOwnProperty(key) && !["info","label","placeholder"].includes(key)) {
            if (typeof obj1[key] === "object" && obj1[key] !== null && typeof obj2[key] === "object" && obj2[key] !== null) {
                // 递归比较嵌套的对象
                const nestedDiff = compareObjects(obj1[key], obj2[key]);

                if (Object.keys(nestedDiff).length > 0) {
                    diff[key] = nestedDiff;
                }
            } else if (obj1[key] !== obj2[key]) {
                // 不同的值
                // diff[key] = {
                //     oldValue: obj1[key],
                //     newValue: obj2[key]
                // };
                diff[key] = obj2[key]
            }
        }
    }

    for (let key in obj2) {
        if (obj2.hasOwnProperty(key) && !obj1.hasOwnProperty(key)) {
            // obj2 有而 obj1 没有的属性
            diff[key] = {
                oldValue: undefined,
                newValue: obj2[key]
            };
        }
    }

    return diff;
}

function isObjectEmpty(obj) {
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            return false; // 对象不为空
        }
    }
    return true; // 对象为空
}

function diffItemValue(defaultItem, cuurentItem) {
    const differences = compareObjects(defaultItem, cuurentItem);
    if (!isObjectEmpty(differences)) {
        return differences
    }
    return {}
}

function getGlobal(template) {
    // const global = JSON.parse(readFile(`./src/theme/${template}/lib/config/settings_schema.json`));
    const global = JSON.parse(readFile(`./src/theme/${template}/config/settings_schema.json`)).global_setting;
    const defaultKeys = [];
    const defaultValue = {};
    defaultGlobal.forEach((item) => {
        item.settings.forEach((el) => {
            if (el.id) {
                defaultKeys.push(el.id);
                defaultValue[el.id] = el;
            }
        });
    });
    const templateKeys = [];

    const diff_global_key = [];

    const diff_global_item = {};

    global.forEach((item, index) => {
        item.settings.forEach((el, idx) => {
            if (el.id) {
                if (templateKeys.includes(el.id)) {
                    console.log(template,"重复", el.id);
                }
                templateKeys.push(el.id);

                if (!defaultKeys.includes(el.id)) {
                    el["key"] = `${index}-${idx}`;
                    diff_global_key.push(el);
                    // writeFile(`./src/theme/${template}/lib/config/diff_global.json`, JSON.stringify(element.sections, "", "\t"));
                    // console.log(template, index, idx, el.id);
                }
                if (defaultValue[el.id]) {
                  const diffValue = diffItemValue(defaultValue[el.id], el);
                  if (!isObjectEmpty(diffValue)) {
                    diff_global_item[el.id] = diffValue;
                  }
                }
                // if(el.default != defaultValue[el.id]){
                //   console.log(el.id,el.default);
                // }
            }
        });
    });
    const findiff = findDifference(defaultKeys, templateKeys);
    
    // writeFile(`./src/theme/${template}/lib/config/diff_global.json`, JSON.stringify({
    //   addConfig:diff_global_key,
    //   diffConfig:diff_global_item,
    //   diffTemplate:findiff.template,
    //   diffDefault:findiff.default,
    // }, "", "\t"));
    
    if (findiff.default.length) {
        console.log(template, " 模版差距", findiff.default);
    }
    if (findiff.template.length) {
        console.log(template, "默认差距", findiff.template);
    }
}

function buildAllGlobal() {
    const list = themeList();
    list.forEach((item) => {
        if (fsExistsSync(`./src/theme/${item}/config`)) {
            getGlobal(item);
        }
    });
}
buildAllGlobal();

// getGlobal("hibaes");
